package com.vincent.service;

import com.vincent.common.enums.ResultMapKey;
import com.vincent.dao.RolePermissionDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by vincent on 15-1-3.
 */
@Service("rolePermissionService")
public class RolePermissionService {
    @Resource
    private RolePermissionDao rolePermissionDao;

    public Map<String,Object> getRolePermissions(Integer id){
        Map<String,Object> map=new HashMap<String, Object>();
        List<Integer> perIds = rolePermissionDao.selectPerIdsByRoleId(id);
        map.put(ResultMapKey.SUCCESS.getCode(),perIds);
        return map;
    }

    public Map<String,Object> grantRolePermissions(Integer roleId,Integer []perIds){
        Map<String,Object> map=new HashMap<String, Object>();
        rolePermissionDao.deleteByRoleIds(new Integer[]{roleId});
        if(perIds.length>1||(perIds.length==1&&perIds[0]!=0)){
            rolePermissionDao.grantRolePermissionIds(roleId,perIds);
        }
        map.put(ResultMapKey.SUCCESS.getCode(),"角色授权成功");
        return map;
    }
}
